home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Networking / UDPSample / dnr.c next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  6.8 KB  |  296 lines  |  [TEXT/KAHL]

  1. /*     DNR.c - DNR library for MPW
  2.  
  3.     (c) Copyright 1988 by Apple Computer.  All rights reserved
  4.     
  5.     Modifications by Jim Matthews, Dartmouth College, 5/91
  6.     
  7.     Further modifications by Steve Falkenburg, Apple MacDTS 8/91
  8.     
  9. */
  10.  
  11. #include <OSUtils.h>
  12. #include <Errors.h>
  13. #include <Files.h>
  14. #include <Resources.h>
  15. #include <Memory.h>
  16. #include <Traps.h>
  17. #include <GestaltEqu.h>
  18. #include <Folders.h>
  19. #include <ToolUtils.h>
  20.  
  21. #define OPENRESOLVER    1L                    /* need to make these longs explicit, even though... */
  22. #define CLOSERESOLVER    2L                    /* ...the prototype below would do it normally.  This*/
  23. #define STRTOADDR        3L                    /* ...is a workaround for a bug in Think C 4.0.5.     */
  24. #define    ADDRTOSTR        4L
  25. #define    ENUMCACHE        5L
  26. #define ADDRTONAME        6L
  27. #define    HINFO            7L
  28. #define MXINFO            8L
  29.  
  30. Handle codeHndl = nil;
  31.  
  32. typedef OSErr (*OSErrProcPtr)(long,...);    /* added proto for Think C compatibility -->SJF<-- */
  33. OSErrProcPtr dnr = nil;                        /* ... otherwise, first arg would be passed as short*/
  34.  
  35.  
  36. TrapType GetTrapType(theTrap)
  37. unsigned long theTrap;
  38. {
  39.     if (BitAnd(theTrap, 0x0800) > 0)
  40.         return(ToolTrap);
  41.     else
  42.         return(OSTrap);
  43.     }
  44.     
  45. Boolean TrapAvailable(trap)
  46. unsigned long trap;
  47. {
  48. TrapType trapType = ToolTrap;
  49. unsigned long numToolBoxTraps;
  50.  
  51.     if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  52.         numToolBoxTraps = 0x200;
  53.     else
  54.         numToolBoxTraps = 0x400;
  55.  
  56.     trapType = GetTrapType(trap);
  57.     if (trapType == ToolTrap) {
  58.         trap = BitAnd(trap, 0x07FF);
  59.         if (trap >= numToolBoxTraps)
  60.             trap = _Unimplemented;
  61.         }
  62.     return(NGetTrapAddress(trap, trapType) != NGetTrapAddress(_Unimplemented, ToolTrap));
  63.  
  64. }
  65.  
  66. void GetSystemFolder(short *vRefNumP, long *dirIDP)
  67. {
  68.     SysEnvRec info;
  69.     long wdProcID;
  70.     
  71.     SysEnvirons(1, &info);
  72.     if (GetWDInfo(info.sysVRefNum, vRefNumP, dirIDP, &wdProcID) != noErr) {
  73.         *vRefNumP = 0;
  74.         *dirIDP = 0;
  75.         }
  76.     }
  77.  
  78. void GetCPanelFolder(short *vRefNumP, long *dirIDP)
  79. {
  80.     Boolean hasFolderMgr = false;
  81.     long feature;
  82.     
  83.     if (TrapAvailable(_GestaltDispatch)) if (Gestalt(gestaltFindFolderAttr, &feature) == noErr) hasFolderMgr = true;
  84.     if (!hasFolderMgr) {
  85.         GetSystemFolder(vRefNumP, dirIDP);
  86.         return;
  87.         }
  88.     else {
  89.         if (FindFolder(kOnSystemDisk, kControlPanelFolderType, kDontCreateFolder, vRefNumP, dirIDP) != noErr) {
  90.             *vRefNumP = 0;
  91.             *dirIDP = 0;
  92.             }
  93.         }
  94.     }
  95.     
  96. /* SearchFolderForDNRP is called to search a folder for files that might 
  97.     contain the 'dnrp' resource */
  98. short SearchFolderForDNRP(long targetType, long targetCreator, short vRefNum, long dirID)
  99. {
  100.     HParamBlockRec fi;
  101.     Str255 filename;
  102.     short refnum;
  103.     
  104.     fi.fileParam.ioCompletion = nil;
  105.     fi.fileParam.ioNamePtr = filename;
  106.     fi.fileParam.ioVRefNum = vRefNum;
  107.     fi.fileParam.ioDirID = dirID;
  108.     fi.fileParam.ioFDirIndex = 1;
  109.     
  110.     while (PBHGetFInfo(&fi, false) == noErr) {
  111.         /* scan system folder for driver resource files of specific type & creator */
  112.         if (fi.fileParam.ioFlFndrInfo.fdType == targetType &&
  113.             fi.fileParam.ioFlFndrInfo.fdCreator == targetCreator) {
  114.             /* found the MacTCP driver file? */
  115.             refnum = HOpenResFile(vRefNum, dirID, filename, fsRdPerm);
  116.             if (GetIndResource('dnrp', 1) == NULL)
  117.                 CloseResFile(refnum);
  118.             else
  119.                 return refnum;
  120.             }
  121.         /* check next file in system folder */
  122.         fi.fileParam.ioFDirIndex++;
  123.         fi.fileParam.ioDirID = dirID;    /* PBHGetFInfo() clobbers ioDirID */
  124.         }
  125.     return(-1);
  126.     }    
  127.  
  128. /* OpenOurRF is called to open the MacTCP driver resources */
  129.  
  130. short OpenOurRF()
  131. {
  132.     short refnum;
  133.     short vRefNum;
  134.     long dirID;
  135.     
  136.     /* first search Control Panels for MacTCP 1.1 */
  137.     GetCPanelFolder(&vRefNum, &dirID);
  138.     refnum = SearchFolderForDNRP('cdev', 'ztcp', vRefNum, dirID);
  139.     if (refnum != -1) return(refnum);
  140.         
  141.     /* next search System Folder for MacTCP 1.0.x */
  142.     GetSystemFolder(&vRefNum, &dirID);
  143.     refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
  144.     if (refnum != -1) return(refnum);
  145.         
  146.     /* finally, search Control Panels for MacTCP 1.0.x */
  147.     GetCPanelFolder(&vRefNum, &dirID);
  148.     refnum = SearchFolderForDNRP('cdev', 'mtcp', vRefNum, dirID);
  149.     if (refnum != -1) return(refnum);
  150.         
  151.     return -1;
  152.     }    
  153.  
  154.  
  155. OSErr OpenResolver(fileName)
  156. char *fileName;
  157. {
  158.     short refnum;
  159.     OSErr rc;
  160.     long rezSize;
  161.     
  162.     if (dnr != nil)
  163.         /* resolver already loaded in */
  164.         return(noErr);
  165.         
  166.     /* open the MacTCP driver to get DNR resources. Search for it based on
  167.        creator & type rather than simply file name */    
  168.     refnum = OpenOurRF();
  169.  
  170.     /* ignore failures since the resource may have been installed in the 
  171.        System file if running on a Mac 512Ke */
  172.        
  173.     /* load in the DNR resource package */
  174.     codeHndl = GetIndResource('dnrp', 1);
  175.     if (codeHndl == nil) {
  176.         /* can't open DNR */
  177.         return(ResError());
  178.         }
  179.     
  180.     DetachResource(codeHndl);
  181.     if (refnum != -1) {
  182.         CloseResFile(refnum);
  183.         }
  184.         
  185.     /* lock the DNR resource since it cannot be reloated while opened */
  186.     MoveHHi(codeHndl);
  187.     HLock(codeHndl);
  188.     dnr = (OSErrProcPtr) *codeHndl;
  189.     
  190.     /* call open resolver */
  191.     rc = (*dnr)(OPENRESOLVER, fileName);
  192.     if (rc != noErr) {
  193.         /* problem with open resolver, flush it */
  194.         HUnlock(codeHndl);
  195.         DisposHandle(codeHndl);
  196.         dnr = nil;
  197.         }
  198.     return(rc);
  199.     }
  200.  
  201.  
  202. OSErr CloseResolver()
  203. {
  204.     if (dnr == nil)
  205.         /* resolver not loaded error */
  206.         return(notOpenErr);
  207.         
  208.     /* call close resolver */
  209.     (void) (*dnr)(CLOSERESOLVER);
  210.  
  211.     /* release the DNR resource package */
  212.     HUnlock(codeHndl);
  213.     DisposHandle(codeHndl);
  214.     dnr = nil;
  215.     return(noErr);
  216.     }
  217.  
  218. OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
  219. char *hostName;
  220. struct hostInfo *rtnStruct;
  221. long resultproc;
  222. char *userDataPtr;
  223. {
  224.     if (dnr == nil)
  225.         /* resolver not loaded error */
  226.         return(notOpenErr);
  227.         
  228.     return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  229.     }
  230.     
  231. OSErr AddrToStr(addr, addrStr)
  232. unsigned long addr;
  233. char *addrStr;                                    
  234. {
  235.     if (dnr == nil)
  236.         /* resolver not loaded error */
  237.         return(notOpenErr);
  238.         
  239.     (*dnr)(ADDRTOSTR, addr, addrStr);
  240.     return(noErr);
  241.     }
  242.     
  243. OSErr EnumCache(resultproc, userDataPtr)
  244. long resultproc;
  245. char *userDataPtr;
  246. {
  247.     if (dnr == nil)
  248.         /* resolver not loaded error */
  249.         return(notOpenErr);
  250.         
  251.     return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
  252.     }
  253.     
  254.     
  255. OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
  256. unsigned long addr;
  257. struct hostInfo *rtnStruct;
  258. long resultproc;
  259. char *userDataPtr;                                    
  260. {
  261.     if (dnr == nil)
  262.         /* resolver not loaded error */
  263.         return(notOpenErr);
  264.         
  265.     return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  266.     }
  267.  
  268.  
  269. extern OSErr HInfo(hostName, returnRecPtr, resultProc, userDataPtr)
  270. char *hostName;
  271. struct returnRec *returnRecPtr;
  272. long resultProc;
  273. char *userDataPtr;
  274. {
  275.     if (dnr == nil)
  276.         /* resolver not loaded error */
  277.         return(notOpenErr);
  278.         
  279.     return((*dnr)(HINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  280.  
  281.     }
  282.     
  283. extern OSErr MXInfo(hostName, returnRecPtr, resultProc, userDataPtr)
  284. char *hostName;
  285. struct returnRec *returnRecPtr;
  286. long resultProc;
  287. char *userDataPtr;
  288. {
  289.     if (dnr == nil)
  290.         /* resolver not loaded error */
  291.         return(notOpenErr);
  292.         
  293.     return((*dnr)(MXINFO, hostName, returnRecPtr, resultProc, userDataPtr));
  294.  
  295.     }    /* removed ; (causes syntax err in Think C 5.0 */
  296.